package threshold;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class LengthOfLongestSubstring {


    //求无重复字符的最长子串
    public int lengthOfLongestSubstring(String s) {
        if(s.length()==1){
            return 1;
        }
        Set<Character> set = new HashSet<>();
        for (int i = 0; i <s.length() ; i++) {
            set.add(s.charAt(i));
        }
        if(s.length()==set.size()){
            return s.length();
        }
        Map<StringBuffer, Integer> map = new HashMap<>();
        StringBuffer strBu = new StringBuffer(s);
        int i;int j;
        for ( i= 0; i <strBu.length() ; i++) {
            StringBuffer strTemp = new StringBuffer();

            for ( j= i; j < strBu.length(); j++) {
                char c = strBu.charAt(j);
                //如果字符c不在无重复的字串中
                if(!isContains(strTemp,c)){
                    strTemp.append(c);
                }else{
                    map.put(strTemp,strTemp.length());
                    break;
                }
            }
            if(j==strBu.length()){
                map.put(strTemp,strTemp.length());
            }

        }


        int max = 0;
        for (Map.Entry<StringBuffer,Integer> entry: map.entrySet()
             ) {
              Integer value = entry.getValue();
            StringBuffer key = entry.getKey();
            System.out.println(key + " " +value);
            if(value>max){
                max = value;
            }
        }
        return max;
    }

    public boolean isContains(StringBuffer s,char a){
        String string = s.toString();
        return string.contains(Character.toString(a));
    }


    public static void main(String[] args) {
        LengthOfLongestSubstring test = new LengthOfLongestSubstring();
        String s = "aab";
        System.out.println(test.lengthOfLongestSubstring(s));
    }
}
